package com.tttrark.coursearrange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tttrark.coursearrange.entity.Teacher;
import com.tttrark.coursearrange.dao.TeacherDao;
import com.tttrark.coursearrange.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tttrark.coursearrange.common.ServerResponse;
import com.tttrark.coursearrange.entity.CoursePlan;
import com.tttrark.coursearrange.entity.CourseInfo;
import com.tttrark.coursearrange.dao.CoursePlanDao;
import com.tttrark.coursearrange.dao.CourseInfoDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
@Slf4j
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, Teacher> implements TeacherService {

    @Resource
    private TeacherDao teacherDao;

    @Resource
    private CoursePlanDao coursePlanDao;

    @Resource
    private CourseInfoDao courseInfoDao;

    @Override
    public Teacher teacherLogin(String username, String password) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getTeacherNo, username)
                .eq(Teacher::getPassword, password);
        return teacherDao.selectOne(wrapper);
    }

    @Override
    public ServerResponse getTeacherSchedule(String semester, String teacherId) {
        try {
            log.info("开始获取教师课表: semester={}, teacherId={}", semester, teacherId);
            
            // 从CoursePlan表中查询该教师在指定学期的所有课程安排
            List<CoursePlan> scheduleList = coursePlanDao.selectList(
                new LambdaQueryWrapper<CoursePlan>()
                    .eq(CoursePlan::getSemester, semester)
                    .eq(CoursePlan::getTeacherNo, teacherId)
                    .orderByAsc(CoursePlan::getClassTime)
            );
            
            if (scheduleList == null || scheduleList.isEmpty()) {
                log.warn("未找到教师课表数据: semester={}, teacherId={}", semester, teacherId);
                return ServerResponse.ofError("未找到该教师的课表数据");
            }

            log.info("找到课程安排数量: {}", scheduleList.size());

            // 获取所有相关的课程编号
            Set<String> courseNos = new HashSet<>();
            for (CoursePlan plan : scheduleList) {
                courseNos.add(plan.getCourseNo());
            }

            log.info("需要查询的课程编号: {}", courseNos);

            // 查询课程信息
            Map<String, CourseInfo> courseMap = new HashMap<>();
            if (!courseNos.isEmpty()) {
                List<CourseInfo> courses = courseInfoDao.selectList(
                    new LambdaQueryWrapper<CourseInfo>()
                        .in(CourseInfo::getCourseNo, courseNos)
                );
                log.info("查询到课程信息数量: {}", courses.size());
                for (CourseInfo course : courses) {
                    courseMap.put(course.getCourseNo(), course);
                    log.info("课程信息: courseNo={}, courseName={}", course.getCourseNo(), course.getCourseName());
                }
            }

            // 组装返回数据
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (CoursePlan plan : scheduleList) {
                Map<String, Object> item = new HashMap<>();
                item.put("id", plan.getId());
                item.put("classTime", plan.getClassTime());
                item.put("weekDay", plan.getClassTime().substring(0, 1)); // 从上课时间中提取星期几
                
                CourseInfo course = courseMap.get(plan.getCourseNo());
                
                if (course != null) {
                    String courseName = course.getCourseName();
                    String className = plan.getClassNo();
                    String displayName = courseName + "(" + className + ")";
                    item.put("displayName", displayName);
                    log.info("组装课程信息: {}", displayName);
                } else {
                    log.error("未找到课程信息: courseNo={}", plan.getCourseNo());
                    item.put("displayName", "未知课程");
                }
                
                resultList.add(item);
            }
            
            log.info("返回课表数据数量: {}", resultList.size());
            return ServerResponse.ofSuccess(resultList);
        } catch (Exception e) {
            log.error("获取教师课表失败", e);
            return ServerResponse.ofError("获取教师课表失败：" + e.getMessage());
        }
    }
}
